using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;



public class PlaygroundController : MonoBehaviour
{
    public EnvironmentController envCtrler;
    public GameObject targetPrefab;
    public GameObject flightAgentPrefab;
    public GameObject badSpherePrefab;

    public List<BadSphere> badSpheres;
    public List<FlightAgent> flightAgents;
    public Transform target;
    public Transform floor;
    private bool isDone; //if all agents in the playground is Done.
    public int doneAgentNum;


    public void Instantiate()
    {
        isDone = false;
        doneAgentNum = 0;

        //Instantiate the target
        target = GameObject.Instantiate(targetPrefab, transform.parent).transform;
        floor = transform.parent.Find("Floor");
        floor.localScale = new Vector3(1,1,((float)envCtrler.epm.GetPara(EP.boundaryForward)-(float)envCtrler.epm.GetPara(EP.boundaryBackward))/10);
        floor.localPosition = new Vector3(0, (float)envCtrler.epm.GetPara(EP.boundaryDown)-10, (float)envCtrler.epm.GetPara(EP.boundaryBackward)+floor.localScale.z*5);
        ResetTarget();

        //Instantiate Agents
        flightAgents = new List<FlightAgent>();
        for(int i=0; i<(int)envCtrler.epm.GetPara(EP.agentNumberPerPlayground); i++)
        {
            GameObject flightAgentObject = GameObject.Instantiate(flightAgentPrefab, transform.parent);
            FlightAgent flightAgent = flightAgentObject.GetComponent<FlightAgent>();
            //flightAgent.Instantiate()
            flightAgent.index = i;
            flightAgent.ResetAgent();
            flightAgents.Add(flightAgent);
        }

        badSpheres = new List<BadSphere>();
        //Instantiate Badspheres
        List<float> posZs = (List<float>)envCtrler.epm.GetPara(EP.badSpheresPosZ);
        foreach(float z in posZs)
        {
            GameObject badSphereObject = GameObject.Instantiate(badSpherePrefab, transform.parent);
            BadSphere badSphere = badSphereObject.GetComponent<BadSphere>();
            //badSphere.Instantiate()
            badSphere.myZ = z;
            badSphere.ResetBadSphere();
            badSpheres.Add(badSphere);
        }
    }

    void Awake()
    {
        envCtrler = GameObject.Find("EnvironmentController").GetComponent<EnvironmentController>();
        this.Instantiate();
    }


    public void PlaygroundReset()
    {
        doneAgentNum = 0;
        isDone = false;
        ResetTarget();
        foreach (FlightAgent fa in flightAgents)
            fa.ResetAgent();
        foreach (BadSphere bs in badSpheres)
            bs.ResetBadSphere();
    }

    public void ResetTarget()
    {
        Vector3 sampledPos = new Vector3((float)envCtrler.epm.GetPara(EP.targetPosX,true),
                                        (float)envCtrler.epm.GetPara(EP.targetPosY,true),
                                        (float)envCtrler.epm.GetPara(EP.targetPosZ));
        target.transform.localPosition = sampledPos;
    }

    public void SetDone(bool value)
    {
        isDone = value;
    }

    public bool GetDone()
    {
        return isDone;
    }
}
